package com.jianpengfei.common.utils;

/*
 * 字符串工具类
 * 
 */
public class StringUtil {

	private StringUtil() {};
	
	/*
	 * 判断传入的源字符串是否大于0
	 */
	public static boolean hasLength(String src) {
		return src != null && src.length()>0;
	}
	
	/*
	 * 判断传入的源字符串是否为非空
	 * 
	 */
	
	public static boolean isNotEmpty(String src) {
		boolean result = false;
		
		//1.判断src是否为null   或者是否为空串  如果是返回false
		result = StringUtil.hasLength(src);
		
		if(!result) {
			return result;
		}else {
			
			//2.判断src中所有字符是否全部为空格  如果是返回false
			//   将字符串转化问哦字符数组
			//  遍历这个字符数组  如果发现一个非空格数组  则将result设置为true 然后退出循环
			
			//重新初始化
			result = false;
			
			char[] temp = src.toCharArray();
			
			for (int i = 0; i < temp.length; i++) {
				if (temp[i] != ' ') {
					result = true;
					break;
				}
			}
		}
		return result;
	}
	
	public static boolean isPhoneNumber(String phone) {
		
		return phone.matches("^1[3|4|5|7|8][0-9]\\d{4,8}$");
	}
	
	public static boolean isAllAlphabet(String src) {
		
		boolean result = false;
		
		if(!StringUtil.isNotEmpty(src)) {
			return result;
		}else {
			char[] temp = src.toCharArray();
			
			for (char c : temp) {
				if(!Character.isAlphabetic(c)) {
					result = false;
					break;
				}
				result = true;
			}
		}
		
		return result;
	}
	
	public static String genRandomString(int n) {
		StringBuffer buffer = new StringBuffer();
		
		for (int i = 0; i < n; i++) {
			char randomChar = (char)((Math.random()*58)+65);
			randomChar = randomChar > 90 && randomChar < 97 ? (char)(randomChar + 8):randomChar;
			buffer.append(randomChar);
		}
		return buffer.toString();
	}
	
	public static String genChineseString(int n) {
		StringBuffer buffer = new StringBuffer();
		
		int start = Integer.valueOf("4e00", 16);
		int end = Integer.valueOf("9fa5", 16);
		

		// 生成中文字符的算法如下：
		for (int i=0; i < n; i++) {
			char randomChar = (char)((Math.random()*(end-start+1))+start);
			buffer.append(randomChar);
		}
		
		return buffer.toString();
	}
	
}
